﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Collections;
using System.Runtime.InteropServices;

using System.Globalization;

namespace System
{
    public class BaseDal : Dal, IBaseDal
    {
        /// <summary>
        /// BaseDal构造
        /// </summary>
        public  BaseDal():base() { }
        
        /// <summary>
        /// BaseDal构造
        /// </summary>
        /// <param name="configName">数据连接在配置文件中的名称</param>
        public  BaseDal(string configName)
            : base(configName)
        { 
        }

        /// <summary>
        /// BaseDal构造
        /// </summary>
        /// <param name="configName">数据连接在配置文件中的名称</param>
        /// <param name="modelAssemblyName">实体对象程序集名</param>
        /// <param name="modelName">实体对象名</param>
        public  BaseDal(string configName, string modelAssemblyName,string modelName)
            : base(configName)
        {
            this.ModelAssemblyName = modelAssemblyName;
            this.ModelName = modelName;
        }

        /// <summary>
        /// BaseDal构造
        /// </summary>
        /// <param name="modelAssemblyName">实体对象程序集名</param>
        /// <param name="modelName">实体对象名</param>
        public  BaseDal(string modelAssemblyName, string modelName)
            : base()
        {
            this.ModelAssemblyName = modelAssemblyName;
            this.ModelName = modelName;
        }

        protected BaseModel _BaseModel;
        protected Type _ModelType;
        protected string _TableName;
        protected string _PrimaryKey;

        protected virtual object CreatObject()
        {
            return CModel.CreatObject(ModelAssemblyName, ModelName); 
        } 

        #region IDO 成员

        #region 属性

        /// <summary>
        /// 对象类型
        /// </summary>
        public virtual Type ModelType
        {
            get
            {
                if (_ModelType == null)
                {
                    _ModelType = BaseModel.GetType();
                }
                return _ModelType;
            }
        }

        /// <summary>
        /// 获取一个当前对应的实体对象
        /// </summary>
        public virtual BaseModel BaseModel
        {
            get
            {
                if (_BaseModel == null)
                {
                    _BaseModel = CreatObject() as BaseModel;
                }
                return _BaseModel;
            }
        }

        /// <summary>
        /// 实体对象程序集名
        /// </summary>
        public virtual string ModelAssemblyName { get; set; }
        
        /// <summary>
        /// 实体对象名
        /// </summary>
        public virtual string ModelName { get; set; }

        /// <summary>
        /// 对象对应表名
        /// </summary>
        public virtual string TableName
        {
            get
            {
                if (string.IsNullOrEmpty(_TableName))
                {
                    _TableName = BaseHelper.GetTableName(ModelName);
                }
                return _TableName;
            }
            set
            {
                _TableName = value;
            }
        }

        /// <summary>
        /// 主键
        /// </summary>
        public virtual string PrimaryKey
        {
            get
            {
                if (_PrimaryKey == null)
                {
                    _PrimaryKey = this.BaseModel.GetPrimaryKey();
                }
                return _PrimaryKey;
            }
            set
            {
                _PrimaryKey = value;
            }
        }
        #endregion

        #region IDAL

        /// <summary>
        /// 判断是否存在符合条件的记录
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  bool Exists(string filter, List<DBParameter> parameters)
        {
            return base.Exists(filter, this.TableName, parameters);
        }

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int GetCount(string filter, List<DBParameter> parameters)
        {
            return base.GetCount(filter, this.TableName, parameters);
        }

        /// <summary>
        /// 判断是否存在符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  bool Exists(List<DbParameter> parameters, string filter)
        {
            return base.Exists(parameters, filter, this.TableName);
        }

        /// <summary>
        /// 获取符合条件的记录数
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int GetCount(List<DbParameter> parameters, string filter)
        {
            return base.GetCount(parameters, filter, this.TableName);
        }
        
        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="filter">筛选条件</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Delete(string filter, List<DBParameter> parameters)
        {
            return base.Delete(filter, this.TableName, parameters);
        }     
        
        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="primaryKeyInfo"></param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Delete(DBParameter primaryKeyInfo)
        {
            return base.Delete(primaryKeyInfo, this.TableName);
        }

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Delete(List<DBParameter> parameters)
        {
            return base.Delete(parameters, this.TableName);
        }      

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Delete(List<DbParameter> parameters, string filter)
        {
            return base.Delete(parameters, filter, this.TableName);
        }

        /// <summary>
        /// 删除符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Delete(List<DbParameter> parameters)
        {
            return base.Delete(this.TableName, parameters);
        }

        /// <summary>
        /// 更新符合条件的记录
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="value">值</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Update(string fieldName, string filter, object value, List<DBParameter> parameters)
        {
            return base.Update(fieldName, filter, this.TableName, value, parameters);
        }

        /// <summary>
        /// 更新符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="fieldName">字段名</param>
        /// <param name="value">值</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  int Update(List<DbParameter> parameters, string filter, string fieldName, object value)
        {
            return base.Update(parameters, fieldName, filter, this.TableName, value);
        }       

        /// <summary>
        /// 获取符合条件的记录
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回结果DataTable</returns>
        public virtual  DataTable GetDataTable(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return base.GetDataTable(fields, filter, sorts, this.TableName, parameters);
        }

        /// <summary>
        /// 以分页形式获取符合条件的记录
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回结果DataTable</returns>
        public virtual  DataTable GetDataTable(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            return base.GetDataTable(pageIndex, pageSize, ref recordCount, fields, filter, sorts, this.TableName, this.PrimaryKey, parameters);
        }

        /// <summary>
        /// 获取符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <returns>返回结果DataTable</returns>
        public virtual  DataTable GetDataTable(List<DbParameter> parameters, string fields, string filter, string sorts)
        {
            return base.GetDataTable(parameters, fields, filter, sorts, this.TableName);
        }

        /// <summary>
        /// 以分页形式获取符合条件的记录
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <returns>返回结果DataTable</returns>
        public virtual  DataTable GetDataTable(List<DbParameter> parameters, int? pageSize, int? pageIndex, ref int? recordCount, string fields, string filter, string sorts)
        {
            return base.GetDataTable(parameters, pageIndex, pageSize, ref recordCount, fields, filter, sorts, this.TableName, this.PrimaryKey);
        }       

        #endregion 

        #endregion

        #region IDO 成员

        #region GetObject

        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");
            fields = BaseHelper.GetDefaultValue(fields, "*");
            string sql;
            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, TableName, base.DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, TableName, base.DBType);
            return GetObject(sql, parameters);
        }

        /// <summary>
        /// 通过ID获取象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(string id, string fields)
        {
            fields = BaseHelper.GetDefaultValue(fields, "*");

            string sql = SqlHelper.FormatSql(fields, PrimaryKey + " =@" + PrimaryKey + "PrimaryKey", TableName, base.DBType);

            List<DBParameter> parameters = new List<DBParameter>();
            DBParameter parameter = new DBParameter(PrimaryKey, PrimaryKey + "PrimaryKey", id);
            parameters.Add(parameter);

            return GetObject(sql, parameters);
        }

        /// <summary>
        /// 通过ID获取象
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(string id)
        {
            return GetObject(id, "");
        }
        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="parameters">参数集合</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(List<DBParameter> parameters, string fields)
        {
            fields = BaseHelper.GetDefaultValue(fields, "*");

            string filter = SqlHelper.GetDefaultFilter(parameters, this.DBType);
            string sql = SqlHelper.FormatSql(fields, filter, this.TableName, base.DBType);

            return GetObject(sql, parameters);
        }

        /// <summary>
        /// 执行 SQL语句获取对象
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(string sql, List<DBParameter> parameters)
        {
            BaseModel model = null;

            using (IDataReader dataReader = ExecuteReader(parameters, sql))
            {
                if (dataReader.Read())
                {
                    model = GetObject(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回对象</returns>
        public virtual  BaseModel GetObject(IDataReader dr)
        {
            SmartDataReader smartdr = new SmartDataReader(dr);
            BaseModel model = CreatObject() as BaseModel;
            model.OnLoading();

            string fieldName;

            for (int i = 0; i < dr.FieldCount; i++)
            {
                fieldName = dr.GetName(i);

                CModel.SetProValue(model, fieldName, SmartReader.GetSmartValue(smartdr, fieldName));
            }

            model.OnLoaded();

            return model;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="dr">DataRow</param>
        /// <returns>返回对象</returns>
        public virtual BaseModel GetObject(DataRow dr)
        {
            SmartDataRow sdrv = new SmartDataRow(dr);
            BaseModel model = CreatObject() as BaseModel;
            model.OnLoading();

            string fieldName;

            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                fieldName = dr.Table.Columns[i].ColumnName;
                CModel.SetProValue(model, fieldName, SmartReader.GetSmartValue(sdrv, fieldName));
            }

            model.OnLoaded();

            return model;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="drv">DataRowView</param>
        /// <returns>返回对象</returns>
        public virtual BaseModel GetObject(DataRowView drv)
        {
            SmartDataRowView sdrv = new SmartDataRowView(drv);
            BaseModel model = CreatObject() as BaseModel;
            model.OnLoading();

            string fieldName;

            for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)
            {
                fieldName = drv.DataView.Table.Columns[i].ColumnName;
                CModel.SetProValue(model, fieldName, SmartReader.GetSmartValue(sdrv, fieldName));
            }

            model.OnLoaded();

            return model;
        }
        #endregion GetObject

        #region GetObjectList

        /// <summary>
        /// 获取全部对象
        /// </summary>
        /// <returns>返回对象集合</returns>
        public virtual  ArrayList GetObjectList()
        {
            return GetObjectList(null, null, null, null);
        }     

        /// <summary>
        /// 执行SQL语句，获取对象集合
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual  ArrayList GetObjectList(string sql, List<DBParameter> parameters)
        {
            IDataReader reader = ExecuteReader(parameters, sql);
            return GetObjectList(reader);
        }
        
        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual  ArrayList GetObjectList(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");

            string sql;

            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, TableName, this.DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, TableName, this.DBType);
            return GetObjectList(sql, parameters);
        }

        /// <summary>
        /// 以分页形式获取符合条件的对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="recordCount">总记录数。为-1时，将从数据库获取该值</param>
        /// <param name="fields">列集合;以","分割,""、null或"*"表示所有列</param>
        /// <param name="filter">筛选条件</param>
        /// <param name="sorts">排序字段及其方式</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回对象集合</returns>
        public virtual  ArrayList GetObjectList(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            string primaryKey = PrimaryKey;

            SqlHelper.FormatArg(ref pageIndex, ref pageSize, ref recordCount, ref fields, ref filter, ref sorts, primaryKey);            

            if (parameters == null) parameters = new List<DBParameter>();

            DBParameter parameter = new DBParameter("", "@RecordCount", recordCount);
            parameter.Size = 4;
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;

            parameters.Add(parameter);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, this.TableName, primaryKey, this.DBType);

            ArrayList list = new ArrayList();


            DbParameterCollection outParameters;

            using (IDataReader dr = ExecuteReader(parameters, sql, out outParameters))
            {
                list = GetObjectList(dr);
            }

            recordCount = Convert.ToInt32(outParameters["@RecordCount"].Value, CultureInfo.InvariantCulture);
         
            return list;

        }

        /// <summary>
        /// 获取对象集合
        /// </summary>
        /// <param name="dr">IDataReader</param>
        /// <returns>返回对象集合</returns>
        public virtual ArrayList GetObjectList(IDataReader dr)
        {
            ArrayList list = new ArrayList();

            using (dr)
            {
                while (dr.Read())
                {
                    list.Add(GetObject(dr));
                }

            }
            return list;
        }

        /// <summary>
        /// 获取对象集合
        /// </summary>
        /// <param name="dt">DataTable</param>
        /// <returns>返回对象集合</returns>
        public virtual ArrayList GetObjectList(DataTable dt)
        {
            ArrayList list = new ArrayList();

            foreach (DataRow r in dt.Rows)
            {
                list.Add(GetObject(r));
            }
            return list;
        }

        /// <summary>
        /// 获取对象集合
        /// </summary>
        /// <param name="dv">DataView</param>
        /// <returns>返回对象集合</returns>
        public virtual ArrayList GetObjectList(DataView dv)
        {
            ArrayList list = new ArrayList();

            foreach (DataRowView dr in dv)
            {
                list.Add(GetObject(dr));
            }

            return list;
        }

        #endregion 

        /// <summary>
        /// 删除指定ID的记录
        /// </summary>
        /// <param name="id">对象ID</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual int Delete(string id)
        {
            List<DBParameter> parameters = new List<DBParameter>();
            DBParameter parameter = new DBParameter(PrimaryKey, PrimaryKey + "PrimaryKey", id);
            parameters.Add(parameter);
            return base.Delete(parameters, this.TableName);
        }

        #endregion  

        protected override T InitSqlInfo<T>(T sqlInfo)
        {
            if (sqlInfo == null)
                sqlInfo = new T();
            if (string.IsNullOrEmpty(sqlInfo.PrimaryKey))
                sqlInfo.PrimaryKey = this.PrimaryKey;
            if (string.IsNullOrEmpty(sqlInfo.TableName))
                sqlInfo.TableName = this.TableName;
            if (string.IsNullOrEmpty(sqlInfo.Sorts))
                sqlInfo.Sorts = PrimaryKey + " ASC";
            return sqlInfo;
        }

        protected override string InitTableName<Tm>()
        {
            return this.TableName;
        }

        protected override string InitTableName(BaseModel model)
        {
            return this.TableName;
        }
        #region IDO 成员


        /// <summary>
        /// 获取符合条件的对象
        /// </summary>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  BaseModel GetObject(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);
            return GetObject(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        /// <summary>
        /// 获取符合条件的对象集合
        /// </summary>
        /// <param name="sqlInfo">封装的筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  ArrayList GetObjectList(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);
            return GetObjectList(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        /// <summary>
        /// 以分页形式获取符合条件的对象集合
        /// </summary>
        /// <param name="pageInfo">封装的分页数据筛选信息</param>
        /// <param name="parameters">参数集合</param>
        /// <returns>返回受影响的记录数</returns>
        public virtual  ArrayList GetObjectList(PageInfo pageInfo, List<DBParameter> parameters)
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);

            int? re = pageInfo.RecordCount;

            ArrayList dt = GetObjectList(pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, parameters);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        #endregion

        #region IEBaseDal 成员

        public virtual  Hashtable GetObjectHash()
        {
            return GetObjectHash(null, null, null, null);
        }

        public virtual  Hashtable GetObjectHash(IDataReader dr)
        {
            Hashtable hash = new Hashtable();

            using (dr)
            {
                while (dr.Read())
                {
                    BaseModel model = GetObject(dr);

                    hash.Add(CModel.GetProValue(model, model.GetPrimaryKey()), model);
                }

            }
            return hash;
        }

        public virtual  Hashtable GetObjectHash(DataTable dt)
        {
            Hashtable hash = new Hashtable();

            foreach (DataRow r in dt.Rows)
            {
                BaseModel model = GetObject(r);

                hash.Add(CModel.GetProValue(model, model.GetPrimaryKey()), model);
            }
            return hash;
        }

        public virtual  Hashtable GetObjectHash(DataView dv)
        {
            Hashtable hash = new Hashtable();

            foreach (DataRowView dr in dv)
            {
                BaseModel model = GetObject(dr);

                hash.Add(CModel.GetProValue(model, model.GetPrimaryKey()),model);
            }

            return hash;
        }

        public virtual  Hashtable GetObjectHash(string sql, List<DBParameter> parameters)
        {
            IDataReader reader = ExecuteReader(parameters, sql);
            return GetObjectHash(reader);
        }

        public virtual  Hashtable GetObjectHash(string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            filter = BaseHelper.GetDefaultValue(filter, "1=1");

            fields = BaseHelper.GetDefaultValue(fields, "*");

            string sql;

            if (string.IsNullOrEmpty(sorts))
                sql = SqlHelper.FormatSql(fields, filter, TableName, this.DBType);
            else
                sql = SqlHelper.FormatSql(fields, filter, sorts, TableName, this.DBType);
            return GetObjectHash(sql, parameters);
        }

        public virtual  Hashtable GetObjectHash(int? pageIndex, int? pageSize, ref int? recordCount, string fields, string filter, string sorts, List<DBParameter> parameters)
        {
            string primaryKey = PrimaryKey;

            SqlHelper.FormatArg(ref pageIndex, ref pageSize, ref recordCount, ref fields, ref filter, ref sorts, primaryKey);

            if (parameters == null) parameters = new List<DBParameter>();

            DBParameter parameter = new DBParameter("", "@RecordCount", recordCount);
            parameter.Size = 4;
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;

            parameters.Add(parameter);

            parameters = InitDataParameter(parameters, pageIndex, pageSize);

            string sql = SqlHelper.GetPageViewSql(pageIndex, pageSize, fields, filter, sorts, this.TableName, primaryKey, this.DBType);

            Hashtable hash = null;


            DbParameterCollection outParameters;

            using (IDataReader dr = ExecuteReader(parameters, sql, out outParameters))
            {
                hash = GetObjectHash(dr);
            }

            recordCount = Convert.ToInt32(outParameters["@RecordCount"].Value, CultureInfo.InvariantCulture);

            return hash;
        }

        public virtual  Hashtable GetObjectHash(SqlInfo sqlInfo, List<DBParameter> parameters)
        {
            sqlInfo = InitSqlInfo<SqlInfo>(sqlInfo);
            return GetObjectHash(sqlInfo.Fields, sqlInfo.Filter, sqlInfo.Sorts, parameters);
        }

        public virtual  Hashtable GetObjectHash(PageInfo pageInfo, List<DBParameter> parameters)
        {
            pageInfo = InitSqlInfo<PageInfo>(pageInfo);

            int? re = pageInfo.RecordCount;

            Hashtable dt = GetObjectHash(pageInfo.PageIndex, pageInfo.PageSize, ref re, pageInfo.Fields, pageInfo.Filter, pageInfo.Sorts, parameters);

            pageInfo.RecordCount = re.Value;

            return dt;
        }

        #endregion
    }
}
